package InputOutputStreamDemo;
import java.io.*;

public class CopyImage {
    public static void main(String[] args) throws Exception {
//        InputStream is = null;
//        OutputStream os = null;
//        try {
//            is = new FileInputStream
//                    ("C:" + File.separator + "Users" + File.separator + "Administrator" + File.separator + "Desktop"
//                    + File.separator + "1" + File.separator + "1.png");
//            os = new FileOutputStream(("C:" + File.separator + "Users" + File.separator + "Administrator" + File.separator + "Desktop"
//                    + File.separator + "2" + File.separator + "1.png"));
//
//            byte[] buffer = new byte[1024];
//            int len;//记住每次读取了多少个字符
//            while ((len= is.read(buffer))!=-1){
//                os.write(buffer,0,len);
//            }
//
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } finally {
//            try {
//                if(is!=null)is.close();
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//            try {
//                if (os!=null)os.close();
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//        }

        /**
         * 更简单的写法不是上述的try catch finally 而是 try with resource
         */
        try (
                InputStream is = new FileInputStream
                ("C:" + File.separator + "Users" + File.separator + "Administrator" + File.separator + "Desktop"
                + File.separator + "1" + File.separator + "1.gif");
                OutputStream os = new FileOutputStream
                ("C:" + File.separator + "Users" + File.separator + "Administrator" + File.separator + "Desktop"
                + File.separator + "2" + File.separator + "1.gif");

                /**
                 * 什么是资源？资源都是会实现AutoCloseable接口，
                 * 使用完后会自动调用close方法完成资源的释放操作
                 */
                //证明try()括号内的资源确实会被自动释放
                MyConnection con = new MyConnection();
        ){
            byte[] buffer = new byte[1024];
            int len;
            while ((len=is.read(buffer))!=-1) {
                os.write(buffer, 0, len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }


//        File f = new File ("C:" + File.separator + "Users" + File.separator + "Administrator" + File.separator + "Desktop"
//                + File.separator + "1" + File.separator + "1.png");
//        long size = f.length();
//        byte[] buffer = new byte[(int) size];

//        String oriFilePath = "C:"+File.separator+"Users"+File.separator+"Administrator"+File.separator+"Desktop"
//                +File.separator+"1"+File.separator+"1.png";
//        String dstFilePath = "C:"+File.separator+"Users"+File.separator+"Administrator"+File.separator+"Desktop"
//                +File.separator+"2"+File.separator+"1.png";
//


//        File oriFile = new File(oriFilePath);
//        File dstFile = new File(dstFilePath);
//            try {
//                OutputStream out = new FileOutputStream(dstFile);
//                InputStream in = new FileInputStream(oriFile);
//
//                int temp = 0;
//                while ((temp = in.read())!=-1){
//                    out.write(temp);//边读边写
//                }
//            } catch (FileNotFoundException e) {
//                throw new RuntimeException(e);
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }

    }
}